//
//  FKMemberChildVC.m
//  WanTao
//
//  Created by Macbook Pro on 2019/3/30.
//  Copyright © 2019 Macbook Pro. All rights reserved.
//

#import "FKMemberChildVC.h"
#import "FKMemberChildCell.h"
#import "FKMemberChild.h"

@interface FKMemberChildVC ()

@property (nonatomic, strong) NSArray *datas;
@property (nonatomic, strong) NSMutableDictionary *param;
@property(nonatomic, assign) NSInteger page;
@property (nonatomic, strong) UIView *emptyView;

@end

@implementation FKMemberChildVC

- (NSArray *)datas
{
    if (!_datas) {
        _datas = [NSArray array];
    }
    return _datas;
}

- (NSMutableDictionary *)param
{
    if (!_param) {
        _param = [NSMutableDictionary dictionary];
        _param[@"pagesize"] = @(10);
    }
    return _param;
}

- (void)setType:(NSString *)type
{
    _type = type;
    
    self.param[@"type"] = self.type;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.separatorInset = UIEdgeInsetsMake(0, 12, 0, 12);
    self.tableView.tableFooterView = [UIView new];
    self.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.tableView.autoresizingMask = UIViewAutoresizingNone;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadData)];
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
}

- (void)setupHeader
{
    if ([self.type isEqualToString:@"31"] ||
        [self.type isEqualToString:@"32"] ||
        [self.type isEqualToString:@"41"] ||
        [self.type isEqualToString:@"42"]) { // 如果是邀请会员，则给tableview加一个header
        UIView *header = [[UIView alloc] init];
        header.backgroundColor = [UIColor whiteColor];
        header.frame = CGRectMake(0, 0, fkScreenW, fk_adjustW(40));
        
        NSArray *titles = @[@"成员", @"上级", @"加入时间"];
        for (NSInteger index = 0; index < titles.count; index++) {
            UILabel *titleLabel = [UILabel fk_labelWithFont:fk_adjustFont(14) textColor:fkColor333333 text:titles[index]];
            [header addSubview:titleLabel];
            titleLabel.tag = 10 + index;
            if (index == 0) {
                [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.offset(12);
                    make.centerY.offset(0);
                }];
            } else if (index == 1) {
                [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.centerX.centerY.offset(0);
                }];
                if ([self.type isEqualToString:@"31"] || [self.type isEqualToString:@"32"]) {
                    titleLabel.hidden = true;
                }
            } else if (index == 2) {
                [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.right.offset(-12);
                    make.centerY.offset(0);
                }];
            }
        }
        UIView *separator = [[UIView alloc] init];
        [header addSubview:separator];
        separator.backgroundColor = [UIColor groupTableViewBackgroundColor];
        [separator mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.bottom.right.offset(0);
            make.height.offset(0.8);
        }];
        
        self.tableView.tableHeaderView = header;
    } else {
        self.tableView.tableHeaderView = [UIView new];
    }
}


- (UIView *)emptyView
{
    if (!_emptyView) {
        _emptyView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, fkScreenW, 200)];
        
        NSString *imgName = @"empty_user";
        if ([self.title containsString:@"用户"]) {
            imgName = @"empty_user";
        } else if ([self.title containsString:@"粉丝"]) {
            imgName = @"empty_fans";
        } else if ([self.title containsString:@"成员"]) {
            imgName = @"empty_user";
        }
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imgName]];
        [_emptyView addSubview:imageView];
        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.offset(0);
            make.width.mas_equalTo(fk_adjustW(imageView.image.size.width));
            make.height.mas_equalTo(fk_adjustW(imageView.image.size.height));
        }];
        
        UILabel *label = [[UILabel alloc] init];
        [_emptyView addSubview:label];
        label.font = fk_adjustFont(16);
        label.text = @"暂无数据!";
        label.textColor = fkColor999999;
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(imageView.mas_bottom).offset(20);
            make.centerX.offset(0);
        }];
    }
    return _emptyView;
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.datas.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FKMemberChildCell *cell = [FKMemberChildCell cellWith:tableView];
    cell.data = self.datas[indexPath.row];
    return cell;
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return fk_adjustW(50);
}

//- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
//{
//
//}


- (void)refreshData
{
    if (self.datas.count == 0) [self.tableView.mj_header beginRefreshing];
}

#pragma mark - 网络请求
- (void)loadData
{
    self.page = 1;
    self.param[@"page"] = @(self.page);
    [FKHttpTool postWithSuffix:@"m=Home&c=Member&a=inviteUserList" param:self.param success:^(FKBaseResult *result) {
        [self.tableView.mj_header endRefreshing];
        if (result.isSuccess) {
            if (self.updateInviteNum) {
                NSMutableDictionary *dict = [NSMutableDictionary dictionary];
                dict[@"invite_num_valid"] = result.data[@"invite_num_valid"];
                dict[@"invite_num_invalid"] = result.data[@"invite_num_invalid"];
                self.updateInviteNum(dict);
            }
            self.datas = [FKMemberChild mj_objectArrayWithKeyValuesArray:result.data[@"list"]];
            for (FKMemberChild *child in self.datas) {
                child.type = [self.type integerValue];
            }
            [self setupHeader];
            [self.tableView reloadData];
            if (self.datas.count > 0) self.page++;
        }
        [self.tableView fk_tableViewCheckEmptyDataWithDataCount:self.datas.count emptyView:self.emptyView];
    } failure:^(NSError *error) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView fk_tableViewCheckEmptyDataWithDataCount:self.datas.count emptyView:self.emptyView];
    }];
}

- (void)loadMoreData
{
    self.param[@"page"] = @(self.page);
    [FKHttpTool postWithSuffix:@"m=Home&c=Member&a=inviteUserList" param:self.param success:^(FKBaseResult *result) {
        [self.tableView.mj_footer endRefreshing];
        if (result.isSuccess) {
            NSArray *newArray = [FKMemberChild mj_objectArrayWithKeyValuesArray:result.data[@"list"]];
            for (FKMemberChild *child in newArray) {
                child.type = [self.type integerValue];
            }
            if (newArray.count > 0) {
                NSMutableArray *array = [NSMutableArray arrayWithArray:self.datas];
                [array addObjectsFromArray:newArray];
                self.datas = array;
                [self.tableView reloadData];
                self.page++;
            } else {
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }
        }
    } failure:^(NSError *error) {
        [self.tableView.mj_footer endRefreshing];
    }];
}


@end
